//
//  JHRegisterViewController.m
//  JHChat
//
//  Created by JiaHao on 2/22/15.
//  Copyright (c) 2015 JiaHao. All rights reserved.
//

#import "JHRegisterViewController.h"
#import "JHXMPPManager.h"
#import "XMPPvCardTemp.h"
#import "XMPPvCardTempModule.h"
@interface JHRegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (strong,nonatomic) XMPPStream *xmppStream;

@end

@implementation JHRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _xmppStream = [JHXMPPManager sharedManager].xmppStream;
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.xmppStream removeDelegate:self];
}


- (IBAction)doRegister:(id)sender {
    [self xmppConnectt];
}


//用用户输入的用户名来创建连接
-(void)xmppConnectt
{
    [self.xmppStream disconnect];
    NSString *userName = self.userNameTextField.text;
    
    //创建JID
    XMPPJID *jid = [XMPPJID jidWithUser:userName domain:XMPP_HOST_NAME resource:XMPP_RESOURCE];
    //把Jid添加到流
    [self.xmppStream setMyJID:jid];
   // [self.xmppStream setHostPort:5222];

    //连接服务器
    NSError *error = nil;
    [self.xmppStream connectWithTimeout:10 error:&error];
    if (error) {
        NSLog(@"连接错误%@",[error localizedDescription]);
    }
}

- (void)updateNickName
{
    XMPPvCardTempModule *module = [JHXMPPManager sharedManager].xmppvCardTempModule;
    XMPPvCardTemp *myvCardTemp = [module myvCardTemp];
    if (!myvCardTemp)
    {
        NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
        XMPPvCardTemp *newvCardTemp = [XMPPvCardTemp vCardTempFromElement:vCardXML];
        [newvCardTemp setNickname:@"nick"];
        [module updateMyvCardTemp:newvCardTemp];
    }
    else
    {
        //Set Values as normal
    }
//    XMPPvCardTemp *myvCardTemp = [[[JHXMPPManager sharedManager] xmppvCardTempModule] myvCardTemp];
//    if (myvCardTemp) {
//        myvCardTemp.nickname = self.userNameTextField.text;
//        [[[JHXMPPManager sharedManager] xmppvCardTempModule] updateMyvCardTemp:myvCardTemp];
//    }
}

//连接成功后,注册用户
-(void)xmppStreamDidConnect:(XMPPStream *)sender
{
    if ([_xmppStream supportsInBandRegistration]) {
        NSString *password = self.passwordTextField.text;
        NSError *error = nil;
        [self.xmppStream registerWithPassword:password error:&error];
        if (error) {
            NSLog(@"注册错误%@",[error localizedDescription]);
        }
    }
}
//注册成功后，认证用户密码
-(void)xmppStreamDidRegister:(XMPPStream *)sender
{
    [self updateNickName];
    [self alterWithTip:@"用户注册成功"];
}

- (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error
{
    [self alterWithTip:@"当前用户已经存在,请直接登录"];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ([alertView.message isEqualToString:@"用户注册成功"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}


//提示框
-(void)alterWithTip:(NSString *) tip
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:tip delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alter show];
}

@end
